/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>供应商管理</h3>
        <h5>网站系统供应商索引与管理</h5>
      </div>
    </div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>供应商管理, 由总平台设置管理.</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <div class="mDiv">
      <div class="ftitle">
        <h3>供应商列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <div class="fbutton">
        <div class="add" @click="toAdd()">
          <span>
            <i class="el-icon-circle-plus" /> 添加供应商
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
    </div>
    <div class="tablebox">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
        <el-table-column label="ID" prop="suppliers_id" width="90" show-overflow-tooltip></el-table-column>
        <el-table-column label="供应商名称" prop="suppliers_name" width="150"></el-table-column>
        <el-table-column label="供应商描述" width="150">
          <template slot-scope="scope">{{scope.row.suppliers_desc || '--'}}</template>
        </el-table-column>
        <el-table-column label="供应商联系人" prop="suppliers_contacts" width="150"></el-table-column>
        <el-table-column label="供应商电话" prop="suppliers_phone" width="150"></el-table-column>
        <el-table-column label="所属店铺ID" prop="store_id" width="150"></el-table-column>
        <el-table-column label="状态" width="100">
          <template slot-scope="scope">
            <div :class="scope.row.is_check == 1?'yes':'no'" @click="update(scope.row)">
              <i :class="scope.row.is_check == 1?'fa fa-check-circle':'fa fa-ban'"></i>
              <span v-text="scope.row.is_check == 1?'是':'否'"></span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="small" @click="toEdit(scope.row)" icon="el-icon-edit-outline">编辑</el-button>
              <el-button
                size="small"
                @click="remove(scope.row.suppliers_id)"
                icon="el-icon-delete"
              >删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
    <el-dialog
      title="供应商新增/编辑"
      :visible.sync="dialogFormVisible"
      width="22%"
      center
      :modal-append-to-body="false"
    >
      <el-form ref="newsCat" :model="form" label-width="140px" class="dialog-form">
        <el-form-item label="供应商名称" required>
          <el-input v-model="form.suppliers_name" size="mini"></el-input>
          <p
            class="err-msg"
            v-if="errResult.suppliers_name && !form.suppliers_name"
          >{{errResult.suppliers_name}}</p>
        </el-form-item>
        <el-form-item label="供应商描述" required>
          <el-input v-model="form.suppliers_desc" size="mini"></el-input>
          <p
            class="err-msg"
            v-if="errResult.suppliers_desc && !form.suppliers_desc"
          >{{errResult.suppliers_desc}}</p>
        </el-form-item>
        <el-form-item label="供应商联系人" required>
          <el-input v-model="form.suppliers_contacts" size="mini"></el-input>
          <p
            class="err-msg"
            v-if="errResult.suppliers_contacts && !form.suppliers_contacts"
          >{{errResult.suppliers_contacts}}</p>
        </el-form-item>
        <el-form-item label="供应商电话" required>
          <el-input v-model="form.suppliers_phone" size="mini"></el-input>
          <p
            class="err-msg"
            v-if="errResult.suppliers_phone && !form.suppliers_phone"
          >{{errResult.suppliers_phone}}</p>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit()" :loading="isLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      article_type: null,
      title: null,
      articleCats: [],
      form: {},
      dialogFormVisible: false,
      errResult: {},
      isLoading: false
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      this.request
        .get("/seller/suppliers/page", {
          params: {
            page: that.currentPage,
            size: that.pageSize
          }
        })
        .then(function(res) {
          that.tableData = res.records;
          that.totalCount = res.total;
        });
    },
    refresh() {
      this.getPage();
      this.$message.success("刷新成功！");
    },
    update(row) {
      var that = this;
      row.is_check = row.is_check == 0 ? 1 : 0;
      this.request.put("seller/suppliers", row).then(function(res) {
        if (res.status == 1) {
          that.getPage();
          that.$message.success("更新成功");
        } else {
          that.$message.error(res.msg);
        }
      });
    },
    remove(id) {
      var that = this;
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          that.request.delete("/seller/suppliers/" + id).then(function(res) {
            if (res.status == 1) {
              that.getPage();
              that.$message.success("删除成功!");
            } else {
              that.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          that.$message.info({
            message: "已取消删除"
          });
        });
    },
    toAdd() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    toEdit(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
    submit() {
      this.isLoading = true;
      var that = this;
      if (this.form.suppliersId) {
        //编辑
        this.request.put("/seller/suppliers", this.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success("保存成功");
            that.getPage();
            that.dialogFormVisible = false;
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        this.request.post("/seller/suppliers", this.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success("添加成功");
            that.getPage();
            that.dialogFormVisible = false;
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
>>> .dialog-form .el-input__inner {
  width: 200px;
}
</style>
